package com.testmanager.testmananger.quantitative.service;

import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.quantitative.constants.Sign;
import com.testmanager.testmananger.quantitative.domain.TestInfoEntity;
import com.testmanager.testmananger.quantitative.domain.WorkerEntity;
import com.testmanager.testmananger.quantitative.dto.TaskInfoDto;
import com.testmanager.testmananger.quantitative.dto.TestInfoDto;
import com.testmanager.testmananger.quantitative.dto.WorkerDto;
import com.testmanager.testmananger.quantitative.mapper.TestInfoMapper;
import com.testmanager.testmananger.quantitative.mapper.WorkerMapper;
import com.testmanager.testmananger.quantitative.repository.TaskInfoRepository;
import com.testmanager.testmananger.quantitative.repository.TestInfoRepository;
import com.testmanager.testmananger.quantitative.repository.WorkerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TestInfoService {

    @Autowired
    WorkerRepository workerRepository;

    @Autowired
    TestInfoRepository testInfoRepository;

    @Autowired
    TaskInfoService taskInfoService;

    @Autowired
    WorkService workService;

    @Autowired
    FlakeIdGenerator flakeIdGenerator ;

    public TestInfoDto getTestInfoWithTaskInfo(long iterationId){

        TestInfoEntity testInftoEntities = testInfoRepository.findByIterationId(iterationId);
        if (testInftoEntities == null) return null;
        List<TaskInfoDto> taskInfoDtos = taskInfoService.getTaskInfoDtos(testInftoEntities.getTestNo());
        List<WorkerDto> workerDtos = workerRepository.findWorkerEntitiesByOwnerNo(testInftoEntities.getTestNo())
                .stream()
                .filter(Objects::nonNull)
                .map(workEntity-> WorkerMapper.toWorkerDto(workEntity))
                .collect(Collectors.toList());
       return TestInfoMapper.toTestInfoDto(testInftoEntities, workerDtos, taskInfoDtos);


    }

    public TestInfoDto getTestInfoWithoutTaskInfo(long iterationId){
        TestInfoEntity testInftoEntities = testInfoRepository.findByIterationId(iterationId);
        if (testInftoEntities == null) return null;
        List<TaskInfoDto> taskInfoDtos = null;
        List<WorkerDto> workerDtos = workerRepository.findWorkerEntitiesByOwnerNo(testInftoEntities.getTestNo())
                .stream()
                .filter(Objects::nonNull)
                .map(workEntity-> WorkerMapper.toWorkerDto(workEntity))
                .collect(Collectors.toList());
        return TestInfoMapper.toTestInfoDto(testInftoEntities, workerDtos, taskInfoDtos);

    }

    public TestInfoDto saveTestInfoWithoutTaskInfo(long iterationId,TestInfoDto testInfoDto){
        log.info("保存测试基础信息：{},迭代id:{}",testInfoDto,iterationId);
        if (testInfoDto.getTestNo() ==0) testInfoDto.setTestNo(flakeIdGenerator.newId());
        TestInfoEntity testInfoEntity = TestInfoMapper.toTestInfoEntity(testInfoDto, iterationId);
        List<WorkerDto> workerDtos = testInfoDto.getWorkers();
       /* workerDtos.stream()
                .filter(Objects::nonNull)
                .forEach(t->{
                    WorkerEntity workerEntity = WorkerMapper.toWorkerEntity(testInfoDto.getTestNo(), t);
                    workerRepository.save(workerEntity);
                });*/
       workService.saveWorkerDtos(workerDtos, testInfoDto.getTestNo());
        testInfoRepository.save(testInfoEntity);
        return getTestInfoWithTaskInfo(iterationId);
    }
}
